﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Nascenture.Models;
using System.Net.Mail;
using System.Net;
namespace Nascenture.Controllers
{
    public class RequestController:Controller
    {
        private NascentureDBContext db = new NascentureDBContext();

        public ActionResult Request()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Request(Request RequestDB)
        {
            if (ModelState.IsValid)
            {
                SendEmail(RequestDB.Area, RequestDB.Email, RequestDB.Name + " <br>" + RequestDB.Phone + "<br>" + RequestDB.Description);
                db.Request.Add(RequestDB);
                db.SaveChanges();
                return View();
            }

            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public Boolean SendEmail(string Subj, string sendEmail, string Msg)
        {

            Boolean result = false;
            string emailFrom = sendEmail;
            string emailTo = System.Configuration.ConfigurationManager.AppSettings["MailFrom"].ToString(); ;
            string SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SMTPServer"].ToString();
            string userid = System.Configuration.ConfigurationManager.AppSettings["SmtpUsername"].ToString();
            string pwd = System.Configuration.ConfigurationManager.AppSettings["SmtpPassword"].ToString();
            string portnumber = System.Configuration.ConfigurationManager.AppSettings["Portnumber"].ToString();

            System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage(emailTo, emailFrom);

            objMail.Subject = Subj;
            objMail.Body = Msg;
            objMail.Bcc.Add(emailTo);

            SmtpClient Client = new SmtpClient(SmtpServer, int.Parse(portnumber));

            System.Net.NetworkCredential basicAuthenticationInfo = new NetworkCredential(userid, pwd);
            Client.UseDefaultCredentials = false;
            Client.Credentials = basicAuthenticationInfo;
            //Client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

            Client.Host = SmtpServer;
            objMail.IsBodyHtml = true;
            try
            {


                Client.Send(objMail);


                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw new InvalidCastException(ex.Message);
            }




        }
    }
}